package com.PQLoad{
	import flash.display.DisplayObject;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;

	public class PQLoad	{
		static private var loadList:Array=[];
		static private var loading:Boolean=false;
		static private var currentParam:LoadVo;
		static private var _loadedSize:int;
		static private var _totalSize:int;
		static private var _totalNum:int;
		static private var _loadedNum:int;
		static private var loader:*;
		public function PQLoad()
		{
		}
		static public function load(param:LoadVo):void{
			if(!param){
				trace("load param isn't null!");
				return;
			}
			if(LoadCache.self.hasCache(param.url)){
				copy(param);
				return;
			}
			loadList.push(param);
			_totalNum++;
			loadList.sortOn("level",Array.DESCENDING);
			if(currentParam&&param.level>currentParam.level){
				loadList.push(currentParam);
				loadList.sortOn("level",Array.DESCENDING);
				loader.stop();
				loadNext();
			}
			if(loading)return;
			startLoad(loadList.shift() as LoadVo);
		}
		static public function get loadedSize():int{
			return _loadedSize;
		}
		static public function get totalSize():int{
			return _totalSize;
		}
		static public function get loadedNum():int{
			return _loadedNum;
		}
		static public function get totalNum():int{
			return _totalNum;
		}
		static private function copy(param:LoadVo):void{
			if(!param.onComplete){
				param.onComplete=copyComplete;
				param.onCompleteParam=param;
			}
			if(FileCheck.checkUrl(param.url)==FileCheck.IMG){
				if(param.onCompleteParam)param.onComplete(LoadCache.self.getCache(param.url).copy(),param.onCompleteParam);
				else param.onComplete(LoadCache.self.getCache(param.url).copy());
			}else if(FileCheck.checkUrl(param.url)==FileCheck.SWF){
				if(param.onCompleteParam)LoadCache.self.getCache(param.url).copy(param.onComplete,param.onCompleteParam);
				else LoadCache.self.getCache(param.url).copy(param.onComplete);
			}else if(FileCheck.checkUrl(param.url)==FileCheck.XML){
				if(param.onComplete){
					if(param.onCompleteParam)param.onComplete(LoadCache.self.getCache(param.url).Data,param.onCompleteParam);
					else param.onComplete(LoadCache.self.getCache(param.url).Data);
				}
			}
		}
		static private function copyComplete(obj:Object,param:LoadVo):void{
			if(param.parent)param.parent.addChild(obj as DisplayObject);
			if(param.width)obj.width=param.width;
			if(param.height)obj.height=param.height;
		}
		static private function loadNext():void{
			loading=false;
			LoadTime.self.remove();
			currentParam=null;
			if(loadList.length<1){
				_totalNum=0;
				_loadedNum=0;
				return;
			}
			startLoad(loadList.shift() as LoadVo);
		}
		static private function startLoad(v:LoadVo):void{
			loading=true;
			LoadTime.self.add(10,loadOutTime);//如果10秒后还没开始加载则按加载超时
			currentParam=v;
			if(FileCheck.checkUrl(v.url)==FileCheck.IMG){
				loader=new ImgLoader(v.url,vo);
			}else if(FileCheck.checkUrl(v.url)==FileCheck.SWF){
				loader=new SwfLoader(v.url,vo);
			}else if(FileCheck.checkUrl(v.url)==FileCheck.XML){
				loader=new XMLLoader(v.url,vo);
			}else{
				trace("未知资源类型，跳过");
				if(currentParam.onError){
					if(currentParam.onErrorParam)currentParam.onError(currentParam.onErrorParam);
					else currentParam.onError();
				}
				loadNext();
			}
		}
		static private function progress(e:ProgressEvent):void{
			_loadedSize=e.bytesLoaded;
			_totalSize=e.bytesTotal;
			if(_totalSize<=0)return;
			var t:int=_totalSize/10000;
			t=t<3?3:t;
			if(LoadTime.self.delay==t)return;
			LoadTime.self.reset(t);//按10K/S计算，重置超时计时
		}
		static private function loadOutTime():void{
			trace("loadOutTime："+currentParam.url);
			loader.stop();
			if(currentParam.onError){
				if(currentParam.onErrorParam)currentParam.onError(currentParam.onErrorParam);
				else currentParam.onError();
			}
			loadNext();
		}
		static private function complete(obj:Object):void{
			trace("loadComplete："+currentParam.url);
			_loadedNum++;
			LoadCache.self.addCache(currentParam.url,loader);
			if(currentParam.parent)currentParam.parent.addChild(obj as DisplayObject);
			if(currentParam.width)obj.width=currentParam.width;
			if(currentParam.height)obj.height=currentParam.height;
			if(currentParam.onComplete){
				if(currentParam.onCompleteParam)currentParam.onComplete(obj,currentParam.onCompleteParam);
				else currentParam.onComplete(obj);
			}
			loadNext();
		}
		static private function error(e:IOErrorEvent):void{
			trace("loadError："+currentParam.url+"  by  "+e.text);
			if(currentParam.onError){
				if(currentParam.onErrorParam)currentParam.onError(currentParam.onErrorParam);
				else currentParam.onError();
			}
			loadNext();
		}
		static private function get vo():Object{
			return {onComplete:complete,onError:error,onProgress:progress};
		}
	}
}